package com.evil.application.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.evil.application.pojo.dto.form.control.option.FormOptionRespDTO;
import com.evil.application.pojo.entity.FormControlOption;
import com.evil.common.application.util.ApplicationRedisKeyUtil;
import com.evil.common.core.constant.RedisConstant;
import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import com.evil.common.core.util.CacheMapperUtil;
import com.evil.common.core.util.StreamUtil;
import org.apache.ibatis.annotations.Param;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author liyang
 * @date 2022-08-08 00:00
 */
public interface FormControlOptionMapper extends BaseMapper<FormControlOption> {

    Optional<FormControlOption> isExistById(Long id);

    default Optional<FormControlOption> isExistById_Cache(Long id) {
        return CacheMapperUtil.cacheById(
                id,
                FormControlOption.class,
                this::findById,
                ApplicationRedisKeyUtil::requestUuidOptionKey,
                RedisConstant.REQUEST_UUID_EXPIRE_IN_SECOND
        );
    }

    default FormControlOption findById(Long id) {
        return this.isExistById(id).orElseThrow(() -> new BusinessException(RCodeEnum.APPLICATION_FORM_CONTROL_OPTION_NOT_EXIST));
    }

    default FormControlOption findById_Cache(Long id) {
        return this.isExistById(id).orElseThrow(() -> new BusinessException(RCodeEnum.APPLICATION_FORM_CONTROL_OPTION_NOT_EXIST));
    }

    List<FormControlOption> findByIds(@Param("ids") Collection<Long> ids, @Param("filterDeleted") boolean filterDeleted);

    List<FormControlOption> findByControlId(@Param("controlId") Long controlId, @Param("filterDeleted") boolean filterDeleted);

    List<FormControlOption> findByControlIds(@Param("controlIds") Collection<Long> controlIds, @Param("filterDeleted") boolean filterDeleted);

    default List<FormControlOption> findByIds_Cache(Collection<Long> ids, boolean filterDeleted) {
        return CacheMapperUtil.cacheByIds(
                ids,
                filterDeleted,
                FormControlOption.class,
                this::findByIds,
                FormControlOption::getOptionId,
                FormControlOption::getIsDeleted,
                null,
                ApplicationRedisKeyUtil::requestUuidOptionKeyMap,
                RedisConstant.REQUEST_UUID_EXPIRE_IN_SECOND
        );
    }

    default Map<Long, FormControlOption> findMapByIds(Collection<Long> ids, boolean filterDeleted) {
        return StreamUtil.toMapK(this.findByIds(ids, filterDeleted), FormControlOption::getOptionId);
    }

    default Map<Long, List<FormControlOption>> findMapByControlIds(Collection<Long> controlIds, boolean filterDeleted) {
        return StreamUtil.groupK(this.findByControlIds(controlIds, filterDeleted), FormControlOption::getControlId);
    }

    /**
     * 获取下拉项列表
     *
     * @param controlIds controlIds
     * @return List
     */
    default List<FormOptionRespDTO> findOptionsByControlIds(Collection<Long> controlIds, boolean filterDeleted) {
        return StreamUtil.transListT(this.findByControlIds(controlIds, filterDeleted), FormOptionRespDTO::new);
    }

    /**
     * 获取下拉项Map
     *
     * @param controlIds controlIds
     * @return Map optionId:FormOptionRespDTO
     */
    default Map<Long, FormOptionRespDTO> findOptionsMapByControlIds(Collection<Long> controlIds, boolean filterDeleted) {
        return StreamUtil.toMapK(this.findOptionsByControlIds(controlIds, filterDeleted), FormOptionRespDTO::getOptionId);
    }

    /**
     * 获取下拉项Map
     *
     * @param controlIds controlIds
     * @return Map controlId:List
     */
    default Map<Long, List<FormOptionRespDTO>> findControlOptionsMapByControlIds(Collection<Long> controlIds, boolean filterDeleted) {
        return StreamUtil.groupK(this.findOptionsByControlIds(controlIds, filterDeleted), FormOptionRespDTO::getControlId);
    }
}